/* $RCSFile: CRMImageView.aspx.cs $
 * Apache license v2 2011 by Christian Surieux
 * $Source: ?/AddonNice.ImageManager/PageModules/CRMImage/CRMImageView.aspx.cs $
 * $log$
 * Revision 20 2010/08/09 10:56:46  christian.surieux
 *   Added Log comment to all .aspx .ascx .master files
 * Revision 7 2010/08/09 09:44:02  christian.surieux
 *   Added Log to cs sources files
 */

using System;
using System.Xml;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;
using System.Globalization;

using AddonNice.Diagnostics;
using AddonNice.Settings;
using AddonNice.Configuration;
using AddonNice.Settings.CRMImage;

using AddonNice.CRMImage;
using AddonNice.CRMImage.UI;
using AddonNice.CRMImage.UI.WebControls;

using Trace2   =   System.Diagnostics.Trace;

namespace AddonNice.PageModules.CRMImage
{
	public partial class CRMImageView : ViewItemImagePage
	{

		/// <summary>
		/// Attempt to cache the layout module name
		/// </summary>
		private string _layoutModule	=	string.Empty;
		string layoutModule
		{
			get
			{
				if (string.IsNullOrEmpty(_layoutModule))
				{
					_layoutModule = Config.ModulesPath + "/CRMImage/Layout/" + moduleSettingsDic[ImStr.ImageLayout].Value;
					if ( PageTraceSwitch.Sw.TraceVerbose )
                        Trace2.WriteLine( string.Format(CultureInfo.InvariantCulture,"[V]CRMImage layoutModule: {0}", _layoutModule),PageTraceSwitch.Sw.Info);
				}
				return _layoutModule;
			}
		}
		/// <summary>
		/// The Page_Load event handler on this Page is used to
		/// obtain obtain the contents of a CRMImage from the 
		/// CRMImage table acces through AddonNice.CRMImageDB() data component to encapsulate 
		/// the data access functionality.
        /// It uses a plug-in layout layer in the form of a dynamically loaded ascx, loaded from the image layout folder
		/// </summary>
		private void Page_Load(object sender, System.EventArgs e)
		{
			if ( PageTraceSwitch.Sw.TraceVerbose )
                Trace2.WriteLine( string.Format(CultureInfo.InvariantCulture,"[V]CRMImageView Page_Load ImageDBID : {0}.", ImageDBID),PageTraceSwitch.Sw.Info);
			if ( !IsPostBack )
			{
				// Obtain a single row of CRMImage information
				SqlDataReader dr = null;

				try
				{
                    dr                          =   ImageDB.GetCRMImage(ImageDBID);
					// Read first row from database
					if ( dr.Read() )
					{
						if ( PageTraceSwitch.Sw.TraceVerbose )
                            Trace2.WriteLine( string.Format(CultureInfo.InvariantCulture,"[V]CRMImageView Page_Load layoutModule : {0}.", layoutModule),PageTraceSwitch.Sw.Info);
						CRMImageItem ImageItem  =   (CRMImageItem)Page.LoadControl(layoutModule);

						Metadata.LoadXml((string)dr[ImStr.DBMetadataXml]);

						XmlElement albumPath    =   Metadata.CreateElement(ImStr.MetaAlbumPath,AlbumPath);

						XmlElement itemCRMID    =   Metadata.CreateElement(ImStr.ItemCrmID,ItemCrmID.ToString());

						XmlElement imageID      =   Metadata.CreateElement(ImStr.ImageDBID,((int)dr[ImStr.DBImageId]).ToString());

						XmlElement moduleID     =   Metadata.CreateElement(ImStr.ModuleID,ModuleID.ToString());

						ImageItem.Metadata = Metadata;
						ImageItem.DataBind();

						Picture.Controls.Add(ImageItem);
					}
				}
				catch (Exception ex)
				{
					if ( PageTraceSwitch.Sw.TraceError )
                        Trace2.WriteLine( string.Format(CultureInfo.InvariantCulture,"[E]CRMImageView Page_Load ex: {0}", ex),PageTraceSwitch.Sw.Info);
				}
				finally
				{
					if (dr != null)
						dr.Close();
				}

				DataBind();
			}
		}

		/// <summary>
		/// Set the module guids with free access to this page
		/// </summary>
		protected override ArrayList AllowedModules
		{
			get
			{
				ArrayList al    =   new ArrayList();
                char[] aa       =   new char[]{'}','{'};
				al.Add(ImStr.CRMImageGuid.Trim(aa));
				al.Add(ImStr.CRMImageSingleGuid.Trim(aa));
				al.Add(ImStr.CRMImageBookGuid.Trim(aa));
				return al;
			}
		}

		#region Web Form Designer generated code

		/// <summary>
		/// Raises OnInitEvent
		/// </summary>
		/// <param name="e"></param>
		override protected void OnInit(EventArgs e)
		{
			// CODEGEN: This call is required by the ASP.NET Web Form Designer.
			InitializeComponent();
			base.OnInit(e);
			
			// We keep only cancel button

			saveAndCloseButton.Visible	=	false;
			saveAndCloseButton2.Visible	=	false;
			updateButton.Visible	=	false;
			updateButton2.Visible	=	false;
		}

		/// <summary>
		/// Required method for Designer support - do not modify
		/// the contents of this method with the code editor.
		/// </summary>
		private void InitializeComponent()
		{
			Load            +=      new System.EventHandler(Page_Load);
		}

		/// <summary>
		///  Necessary to have the traditionnal buttons automatically added to page
		/// using the PlaceHolders
		/// </summary>
		protected override System.Web.UI.WebControls.PlaceHolder placeHolderForButtons
		{
			get
			{ return PlaceHolderButtons; }
			set { }
		}

        protected override System.Web.UI.WebControls.PlaceHolder placeHolderForButtons2
		{
			get
			{ return PlaceHolderButtons2; }
			set { }
		}

		#endregion
	}
}